package abc.database.interfaces;

import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public interface SqLiteHelper extends ISqlHelper<Map<String, Object>> {
    default List<Map<String, Object>> getTableList(String dbname, JdbcTemplate jdbcTemplate) {
        String sql = "SELECT name as table_name, type, '' as table_comment FROM sqlite_master WHERE type='table' or  type='view' ORDER BY name;";
        return jdbcTemplate.queryForList(sql);
    }

    default List<Map<String, Object>> getColumnList(String dbname, JdbcTemplate jdbcTemplate) {
        List<Map<String, Object>> columns = new ArrayList<>();
        List<Map<String, Object>> tableList = this.getTableList(dbname, jdbcTemplate);
        if(tableList == null) {
            return columns;
        }
        tableList.forEach(a-> {
            if(!a.containsKey("table_name") || a.get("table_name")==null) {
                return;
            }
            String tablename = a.get("table_name").toString();
            String sql = String.format("PRAGMA table_info(\"%s\")", tablename);
            List<Map<String, Object>> cols = jdbcTemplate.queryForList(sql);
            if(cols!=null && cols.size()>0) {
                columns.addAll(cols);
            }
        });

        return columns;
    }
}
